package com.jic.market.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.RestResult;
import com.jic.market.entity.MarketConfigEntity;
import com.jic.market.mapper.MarketConfigEntityMapper;
import com.jic.market.request.MarketConfJumpLinksRequest;
import com.jic.market.response.MarketConfJumpLinksResponse;
import com.jic.market.service.MarketConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 营销中心配置表
 * @author : 
 * @email : 
 * @since : 2020年7月8日
 * @version : v1.0.0
 */
@Service
public class MarketConfigServiceImpl implements MarketConfigService {

    @Autowired
    private MarketConfigEntityMapper marketConfigEntityMapper;

    private final String LOTTERYJUMPLINKADR = "lottery_jump_link_adr";

    private final String LOTTERYJUMPLINKIOS = "lottery_jump_link_ios";

    private final String LOTTERYJUMPLINKH5 = "lottery_jump_link_h5";

    private final String LOTTERYJUMPLINKS = "lottery_jump_links";

    @Override
    public long insert(MarketConfigEntity marketConfigEntity) {
        long id = marketConfigEntityMapper.insert(marketConfigEntity);
        return id;
    }


    @Override
    public long update(MarketConfigEntity marketConfigEntity) {
        return marketConfigEntityMapper.update(marketConfigEntity);
    }

    @Override
    public RestResult thirdPartyJumpLinks(MarketConfJumpLinksRequest request) {
        AdminOperate adminOperate = request.getAdminOperate();
        MarketConfigEntity marketConfigEntity = new MarketConfigEntity();
        marketConfigEntity.setDeptId(adminOperate.getDeptId());
        marketConfigEntity.setConfigType(LOTTERYJUMPLINKS);
        List<MarketConfigEntity> list = marketConfigEntityMapper.findAll(marketConfigEntity);
        if(CollectionUtils.isNotEmpty(list)){
            for(MarketConfigEntity entity : list){
                if(LOTTERYJUMPLINKADR.equals(entity.getConfigKey())){
                    entity.setConfigValue(request.getAndroidConfigValue());
                    entity.setUpdator(adminOperate.getOperator());
                    entity.setUpdatorName(adminOperate.getOperateName());
                    marketConfigEntityMapper.update(entity);
                }
                if(LOTTERYJUMPLINKIOS.equals(entity.getConfigKey())){
                    entity.setConfigValue(request.getIosConfigValue());
                    entity.setUpdator(adminOperate.getOperator());
                    entity.setUpdatorName(adminOperate.getOperateName());
                    marketConfigEntityMapper.update(entity);
                }
                if(LOTTERYJUMPLINKH5.equals(entity.getConfigKey())){
                    entity.setConfigValue(request.getH5ConfigValue());
                    entity.setUpdator(adminOperate.getOperator());
                    entity.setUpdatorName(adminOperate.getOperateName());
                    marketConfigEntityMapper.update(entity);
                }
            }
            return RestResult.success("编辑成功");
        }else{
            List<MarketConfigEntity> entityList = new ArrayList<>();
            if(StringUtils.isNotEmpty(request.getAndroidConfigValue())){
                MarketConfigEntity entity = new MarketConfigEntity();
                entity.setConfigKey(LOTTERYJUMPLINKADR);
                entity.setConfigValue(request.getAndroidConfigValue());
                entity.setConfigType(LOTTERYJUMPLINKS);
                entity.setCreator(adminOperate.getOperator());
                entity.setCreatorName(adminOperate.getOperateName());
                entity.setDeptId(adminOperate.getDeptId());
                entity.setDeleteFlag(0);
                entityList.add(entity);
            }

            if(StringUtils.isNotEmpty(request.getIosConfigValue())){
                MarketConfigEntity entity = new MarketConfigEntity();
                entity.setConfigKey(LOTTERYJUMPLINKIOS);
                entity.setConfigValue(request.getIosConfigValue());
                entity.setConfigType(LOTTERYJUMPLINKS);
                entity.setCreator(adminOperate.getOperator());
                entity.setCreatorName(adminOperate.getOperateName());
                entity.setDeptId(adminOperate.getDeptId());
                entity.setDeleteFlag(0);
                entityList.add(entity);
            }

            if(StringUtils.isNotEmpty(request.getH5ConfigValue())){
                MarketConfigEntity entity = new MarketConfigEntity();
                entity.setConfigKey(LOTTERYJUMPLINKH5);
                entity.setConfigValue(request.getH5ConfigValue());
                entity.setConfigType(LOTTERYJUMPLINKS);
                entity.setCreator(adminOperate.getOperator());
                entity.setCreatorName(adminOperate.getOperateName());
                entity.setDeptId(adminOperate.getDeptId());
                entity.setDeleteFlag(0);
                entityList.add(entity);
            }
            if(CollectionUtils.isNotEmpty(entityList)){
                marketConfigEntityMapper.foreachInsert(entityList);
            }
            return RestResult.success("新增成功");
        }
    }

    @Override
    public RestResult<MarketConfJumpLinksResponse> getDeptJumpLinks(MarketConfJumpLinksRequest request) {
        AdminOperate adminOperate = request.getAdminOperate();
        MarketConfigEntity marketConfigEntity = new MarketConfigEntity();
        marketConfigEntity.setDeptId(adminOperate.getDeptId());
        marketConfigEntity.setConfigType(LOTTERYJUMPLINKS);
        List<MarketConfigEntity> list = marketConfigEntityMapper.findAll(marketConfigEntity);
        MarketConfJumpLinksResponse response = new MarketConfJumpLinksResponse();
        if(CollectionUtils.isNotEmpty(list)){
            for(MarketConfigEntity entity : list){
                if(LOTTERYJUMPLINKADR.equals(entity.getConfigKey())){
                    response.setAndroidConfigValue(entity.getConfigValue());
                }
                if(LOTTERYJUMPLINKIOS.equals(entity.getConfigKey())){
                    response.setIosConfigValue(entity.getConfigValue());
                }
                if(LOTTERYJUMPLINKH5.equals(entity.getConfigKey())){
                    response.setH5ConfigValue(entity.getConfigValue());
                }
            }
        }
        return RestResult.success(response);
    }
}